package com.zhentao.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhentao.pojo.TbMessage;
import com.zhentao.ws.WebSocketSessionManager;
import org.springframework.web.bind.annotation.*;

/**
 * 推送控制器：供其他服务（如stu_service）HTTP调用以向教师端WS会话推送消息
 */
@RestController
@RequestMapping("/push")
public class PushController {

    private final ObjectMapper objectMapper = new ObjectMapper();

    @PostMapping("/message")
    public String pushMessage(@RequestBody TbMessage message) throws JsonProcessingException {
        String payload = objectMapper.writeValueAsString(message);
        boolean ok = WebSocketSessionManager.sendTo(message.getReceiverType(), message.getReceiverId(), payload);
        return ok ? "PUSH_OK" : "PUSH_NO_SESSION";
    }
}